<template>
  <md-popup v-model="value" position="bottom" :has-mask="true" prevent-scroll @hide="cancel">
    <md-popup-title-bar title="匿名评价" cancel-text="取消" @cancel="cancel"></md-popup-title-bar>
    <div class="evaluate_box">
      <div class="starWrap">
        <img :src="level>index?stara:starb" v-for="(item,index) in levelList" :key="index" @click="getlevel(index)" />
        <p v-if="Isevalute=='0'">选择星级评价</p>
        <p v-if="Isevalute=='1'">{{evaluateResulttext}}</p>
      </div>
      <div v-if="Isevalute=='1'">
        <ul class="evaluationList">
          <li
            v-for="(item, index) in evaluationList"
            :key="index"
            @click="getevaluationList(index,item)"
            :class="{'evaluation-active':evaluationArr[index]}"
            ref="menuItem"
          >{{item.levelLabelName}}</li>
        </ul>
      </div>
      <div style="clear:both"></div>
      <p class="taxi_pay" v-if="Isevalute=='1'" @click="incityEvaluteBtn">匿名提交</p>
    </div>
  </md-popup>
</template>
<script>
import { Popup, PopupTitleBar, Picker, Selector, Dialog, Toast } from 'mand-mobile';
import { mapGetters, mapActions, mapMutations } from 'vuex';
import Loading from '@/components/loading';
import http from '@/common/http';
var moment = require('moment');
moment.locale('zh-cn');
export default {
  name: 'time-selector',
  components: {
    [Popup.name]: Popup,
    [PopupTitleBar.name]: PopupTitleBar,
    [Selector.name]: Selector,
    [Picker.name]: Picker,
    [Toast.name]: Toast
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    appendTo: {
      default: () => document.body
    }
  },
  data() {
    return {
      Isevalute: '0',
      evaluationArr: [],
      evaluationnewArr: [],
      evaluationDescribe: '',
      evaluateResulttext: '',
      level: '0',
      levelList: [0, 1, 2, 3, 4],
      stara: require('@/assets/imgs/xcxq_icon_xing_light@2x.png'), // 亮星星
      starb: require('@/assets/imgs/xcxq_icon_xing_dark@2x.png'), // 暗星星
      evaluationList: []
    };
  },
  created() {},
  methods: {
    cancel() {
      this.value = false;
    },
    // 获取评价星星
    getlevel(val) {
      this.evaluationList = [];
      this.evaluationnewArr = [];
      this.evaluationArr = [];
      this.Isevalute = '1';
      this.level = val + 1;
      for (var i in this.getIncityOrderDate.apiOrderEvaluate) {
        if (this.getIncityOrderDate.apiOrderEvaluate[i].level === this.level.toString()) {
          this.evaluateResulttext = this.getIncityOrderDate.apiOrderEvaluate[i].levelText;
          this.evaluationList = this.getIncityOrderDate.apiOrderEvaluate[i].levelSpanList;
        }
      }
      // for (var k in this.levelSpanList) {
      //     this.evaluationList.push(this.levelSpanList[k].levelLabelName)
      // }
      this.putevaluation();
    },
    getevaluationList(index, item) {
      for (let i = 0; i < this.evaluationArr.length; i++) {
        if (i === index) {
          this.evaluationArr[index] ? this.evaluationArr.splice(index, 1, false) : this.evaluationArr.splice(index, 1, true);
          if (this.evaluationArr[index]) {
            this.evaluationnewArr.push(this.evaluationList[index].levelLabelId);
          } else {
            this.removeArr(item.levelLabelId);
          }
        }
      }
      console.log(this.evaluationnewArr);
    },
    queryIndexOf(item) {
      for (var i = 0; i < this.evaluationnewArr.length; i++) {
        if (this.evaluationnewArr[i] === item) {
          return i;
        }
      }
      return -1;
    },
    removeArr(item) {
      var index = this.queryIndexOf(item);
      if (index > -1) {
        this.evaluationnewArr.splice(index, 1);
      }
    },
    putevaluation() {
      for (let i = 0; i < this.evaluationList.length; i++) {
        this.evaluationArr.push(false);
      }
    },
    // 提交评价
    incityEvaluteBtn() {
      if (this.evaluationnewArr.join(',').length === 0) {
        Toast({
          content: '请选择评价标签',
          duration: 3000,
          hasMask: true
        });
        return false;
      }
      let params = {
        orderNo: this.$route.query.orderno,
        evaluateLabel: this.evaluationnewArr.join(','), // 评价标签
        evaluateLevel: this.level.toString(), // 星级评价
        evaluateLevelText: this.evaluateResulttext // 星级评价描述
      };
      Loading.show();
      http.post('/pakj/city/saveOrderEvaluate', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          Dialog.alert({
            title: '提示',
            content: '评价成功',
            confirmText: '确定',
            onConfirm: () => {
              this.$emit('submit');
            }
          });
        } else {
          Dialog.alert({
            title: '提示',
            content: res.data.msg,
            confirmText: '确定'
          });
        }
      });
    }
  },
  mounted() {
    if (this.appendTo) {
      this.appendTo.appendChild(this.$el);
    }
  },
  beforeDestroy() {
    if (this.appendTo) {
      this.appendTo.removeChild(this.$el);
    }
  },
  computed: {
    ...mapGetters(['getIncityOrderDate'])
  }
};
</script>

<style lang="stylus">
.evaluate_box {
  background-color: #fff;
  padding: 30px 0px;
}

.starWrap {
  width: 100%;
  text-align: center;
  margin-top: 30px;
}

.starWrap img {
  width: 50px;
  height: 50px;
  margin-left: 30px;
}

.starWrap p {
  font-size: 26px;
  color: #666;
  margin-top: 10px;
}

.evaluationList li {
  height: 72px;
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  float: left;
  text-align: center;
  line-height: 72px;
  margin-left: 5%;
  margin-top: 20px;
  font-size: 28px;
  color: #666;
  padding: 0px 25px;
}

.evaluationList li.evaluation-active {
  border: 1px solid #ff6917;
  color: #ff6917;
}

.taxi_pay {
  background-color: #ff6917;
  height: 98px;
  line-height: 98px;
  font-size: 36px;
  color: #fff;
  text-align: center;
  margin: 20px 20px 0px 20px;
  border-radius: 7px;
}
</style>
